<?php
/**
 * Created by PhpStorm.
 * User: kb178
 * Date: 2017/9/14
 * Time: 16:48
 */
namespace soa\kbUser;
use action\v1\shop\update;
use soa\client;
use artisan\cache;
use soa\common\Remote;
class kbUser extends client{
    private $cache;
    private $key = "kbUserId:info";
    //这里添加账户类型时\kuaidihelp_user\app\action\v1\kbuser\KbUser.php 对应的类型也要添加上
    //账户类型 s快递员 c微快递 ws微商 yz 驿站 v_home 微掌柜 shop 网点
    private $type = array('s','c','ws','yz','v_home','shop');
    function __construct(){
        $this->cache = cache::connect('base2');
    }
    public function createId($kbType){
        if(!in_array($kbType,$this->type)){
            raise(1009,'用户类型错误！soa');
        }
        $type = "v1/kbuser/KbUser/createId";
        $data['type'] = $kbType;
        $result = $this->response($type,$data);
        return $result;
    }
    /**
     * 查询用户信息
     * $kbId 快宝用户唯一ID
     * return array
    **/
    public function getKbUserInfo($kbId){
        if(!$kbId){
            raise(1009);
        }
        $kbUserInfo = $this->cache->hGetAll($this->key.$kbId);
        if(!$kbUserInfo){
            $type = "v1/kbuser/KbUser/getKbUserInfo";
            $data['kbId'] = $kbId;
            $kbUserInfo = $this->response($type,$data);
            $kbUserInfo = json_encode($kbUserInfo);
            $this->cache->hset($this->key,$kbId,$kbUserInfo);
            $this->cache->expire($this->key.$kbId,"604800"); //缓存设置一周
        }
        $kbUserInfo = json_decode($kbUserInfo,true);
        return $kbUserInfo;
    }
    /**
     * 快递员用户数据添加唯一ID
    **/
    public function updateCourier($courierId,$kbId){
        if(!$courierId || !$kbId){
            raise(1009);
        }
        $type = "v1/kbuser/KbUser/updateCourier";
        $data['kbId'] = $kbId;
        $data['courierId'] = $courierId;
        $ret = $this->response($type,$data);
        return $ret;
    }
    /**
     * 网点用户创建唯一ID
    **/
    public function updateShopAccount($id,$kbId){
        if(!$id || !$kbId){
            raise(1009);
        }
        $type = "v1/kbuser/KbUser/updateShopAccount";
        $data['kbId'] = $kbId;
        $data['shopId'] = $id;
        $ret = $this->response($type,$data);
        return $ret;
    }

    /**
     * 微快递用户创建唯一ID
     **/
    public function updateUserAccount($id,$kbId){
        if(!$id || !$kbId){
            raise(1009);
        }
        $type = "v1/kbuser/KbUser/updateUserAccount";
        $data['kbId'] = $kbId;
        $data['userId'] = $id;
        $ret = $this->response($type,$data);
        return $ret;
    }
    private  function response($type,$data){
        $remote = new Remote('account.core');
        $result = $remote->doPost($type, $data);
        if($remote->getCode()=='0'){
            return $result;
        }else{
            raise($remote->getCode(),$remote->getMsg(),array());
        }
    }
}
?>